<template>
  <div class="discovery-container">

    <!-- 轮播图 -->
    <el-carousel class :interval="4000" type="card">
      <el-carousel-item v-for="(item, index) in banners" :key="index">
        <img :src="item.imageUrl" alt />
      </el-carousel-item>
    </el-carousel>

    <!-- 推荐歌单 -->
    <div class="recommend">
      <h3 class="title">推荐歌单</h3>
      <div class="items">
        <div class="item" v-for="(item,index) in recommendedSongList" :key="index">
          <div class="img-wrap">
            <div class="desc-wrap">
              <span class="desc">{{item.name}}</span>
            </div>
            <img :src="item.picUrl" alt />
            <span class="iconfont icon-play" @click="toMusicList(item.id)"></span>
          </div>
          <p class="name">{{item.name}}</p>
        </div>
      </div>
    </div>
    <!-- 最新音乐 -->
    <div class="news">
      <h3 class="title">最新音乐</h3>
      <div class="items">
        <div class="item" v-for="(item,index) in musics" :key="index">
          <div class="img-wrap">
            <img :src="item.picUrl" alt />
            <span @click="playMusic(item.id)" class="iconfont icon-play"></span>
          </div>
          <div class="song-wrap">
            <div class="song-name">{{item.name}}</div>
            <div class="singer">{{item.song.artists[0].name}}</div>
          </div>
        </div>
      </div>
    </div>
    <!-- 推荐MV -->
    <div class="mvs">
      <h3 class="title">推荐MV</h3>
      <div class="items">
        <div class="item" v-for="(item,index) in mvs" :key="index">
          <div class="img-wrap">
            <img :src="item.picUrl" alt />
            <span class="iconfont icon-play" @click="toMv(item.id)"></span>
            <div class="num-wrap">
              <div class="iconfont icon-play"></div>
              <div class="num">{{item.playCount}}</div>
            </div>
          </div>
          <div class="info-wrap">
            <div class="name">{{item.name}}</div>
            <div class="singer">{{item.artisName}}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "discovery",
  data() {
    return {
      banners: [],
      recommendedSongList: [],
      musics: [],
      mvs: [],
    };
  },
  methods: {
    toMv(id) {
      this.$router.push(`/mv?id=${id}`);
    },
    playMusic(id) {
      axios({
        // url: " https://autumnfish.cn/song/url",
        url: "/autumnfish/song/url",
        method: "get",
        params: { id },
      }).then((res) => {
        let url = res.data.data[0].url;
        if (!url) {
          this.$message.error("无法播放这首歌，可能因为没有版权");
        }
        this.$parent.musicUrl = url;
        this.$parent.volume = 0.2;
      });
    },
    toMusicList(id) {
      this.$router.push("/musicList?id=" + id);
    },
  },
  created() {

    // 轮播图
    axios({
      // url: "https://autumnfish.cn/banner",
      url: "/autumnfish/banner",
      method: "get",
    }).then((res) => {
      this.banners = res.data.banners;
    });

    // 推荐歌单
    axios({
      // url: "https://autumnfish.cn/personalized",
      url: "/autumnfish/personalized",
      method: "get",
      params: { limit: 10 },
    }).then((res) => {
      this.recommendedSongList = res.data.result;
    });

    // 最新音乐
    axios({
      // url: "https://autumnfish.cn/personalized/newsong",
      url: "/autumnfish/personalized/newsong",
      method: "get",
    }).then((res) => {
      this.musics = res.data.result;
    });

    // 推荐MV
    axios({
      // url: " https://autumnfish.cn/personalized/mv",
      url: "/autumnfish/personalized/mv",
      method: "get",
    }).then((res) => {
      this.mvs = res.data.result;
    });
  },
};
</script>

<style >
</style>
